//
//  MedSearch.m
//  Pocket Doctor alpha version
//
//  Created by Sherif Nosseir on 11-12-06.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//
//add all medications from database in DataGenerator
//Implement with add medicine
//Clear history when pressing back to table.
//make activity indicator disappear when in table view.

#import "DataGenerator.h"
#import "MainMenu.h"

#import "MedSearch.h"

@implementation MedSearch
@synthesize tableContent, webView, navigationComplete, navigationLoading, lastPageLoading, nextPageLoading, stopLoading, googlepedia, webBar, backToGooglepedia, backMainMenu, lastPage, nextPage, activityIndicator, content, indices;
@synthesize mm, dg;

- (IBAction)BackToMainMenu:(id)sender{
    [self dismissModalViewControllerAnimated:YES];
}

- (IBAction)BackToGooglepedia:(id)sender{
    tableContent.hidden = false;
    googlepedia.hidden = false;
    webView.hidden = true;
    webBar.hidden = true;
}

//refresh = go back to google places
//stop would stop the webview from loading the next/current page.
- (IBAction)RefreshOrStop:(id)sender{
    // reconstruct requestObj here, or use a class member
    if ([webView isLoading]){
        [webView stopLoading];
    }
}

//go back in stack (built in)
- (IBAction)WebsiteBack:(id)sender{
    if ([webView canGoBack]) {
        [webView goBack];
    }
}

//go forward in stack (built in)
- (IBAction)WebsiteForward:(id)sender{
    if ([webView canGoForward]) {
        [webView goForward];
    }
}

//animation when loading web page
- (void)webViewDidStartLoad:(UIWebView *)webView{
    navigationLoading.hidden = false;
    navigationComplete.hidden = true;
    activityIndicator.hidden = false;
    [activityIndicator startAnimating];
}

//finish animations when web page is loaded
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    navigationLoading.hidden = true;
    navigationComplete.hidden = false;
    [activityIndicator stopAnimating];
    activityIndicator.hidden = true;
}

//lost connection of failed to load a page
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    navigationLoading.hidden = true;
    navigationComplete.hidden = false;    
    [activityIndicator stopAnimating];    
    activityIndicator.hidden=true;
    
}


// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [content count];
}

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[[content objectAtIndex:section] objectForKey:@"rowValues"] count] ;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                       reuseIdentifier:CellIdentifier] autorelease];
    }
    cell.textLabel.text = [[[content objectAtIndex:indexPath.section] objectForKey:@"rowValues"]
                           objectAtIndex:indexPath.row];
    
    return cell;
}

- (void) tableView:(UITableView *) tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    tableContent.hidden = true;
    googlepedia.hidden = true;
    webView.hidden = false;
    webBar.hidden = false;
    
    UITableViewCell *queryCell = [tableView cellForRowAtIndexPath:indexPath];
    
    NSString *searchQuery = [[NSString alloc]initWithFormat:@"http://www.google.com/search?q=%@", queryCell.textLabel.text];
    
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:searchQuery]]];
    
    [searchQuery release];
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return [content valueForKey:@"headerTitle"];
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    return [indices indexOfObject:title];
}

- (NSString *)tableView:(UITableView *)aTableView titleForHeaderInSection:(NSInteger)section {
	return [[content objectAtIndex:section] objectForKey:@"headerTitle"];
    
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    if (dg == nil) {
        DataGenerator *dgObject = [[DataGenerator alloc]init];
        dg = dgObject;
        
        [dgObject release];
    }
    content = [dg wordsFromLetters];
    indices = [[content valueForKey:@"headerTitle"] retain];
    
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
